class_name  WeaponC_Area2DDetect
extends WeaponC

@export var detectors:Array[Area2DDetector] = []
var areas:Array[Area2D] = []
var detector_dict:Dictionary = {}

func get_area2d(index:int):
	if index < areas.size():
		return areas[index]
func get_detector(detector_id:int):
	return detector_dict.get(detector_id)
func switch_area2d_active(index:int,is_active:bool):
	if index < areas.size():
		areas[index].monitoring = is_active
		areas[index].monitorable = is_active
func clear_detector_detect_target_dict(detector_id:int):
	var detector:Area2DDetector = get_detector(detector_id)
	if detector != null:
		detector.clear_detect_target_dict()
func set_detector_callback(detector_id:int,callback,is_connect:bool):
	var detector:Area2DDetector = get_detector(detector_id) as Area2DDetector
	if detector != null:
		detector.set_on_detect_target(callback,is_connect)
func init_component(weapon:Weapon):
	super.init_component(weapon)
	areas.clear()
	for child in get_children():
		if child is Area2D:
			areas.append(child)
	for detector in detectors:
		detector_dict[detector.detector_id] = detector
func equip():
	super.equip()
func unequip():
	super.unequip()
func discard():
	super.discard()
	
func _init() -> void:
	weapon_component_name = "WeaponC_Area2DDetect"
func _process(delta: float) -> void:
	if not weapon or not weapon.enable or not enable or not inited:
		return
	for detector in detectors:
		if detector.timing == GameEnum.Area2DDetectorTiming.Stay and detector.on_detect_target.get_connections().size()>0:
			detector.detect_update()
